package com.viphuli.common;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.webkit.JavascriptInterface;

import com.offroader.share.wxapi.ShareUtils;
import com.offroader.utils.OnlineParamUtils;
import com.offroader.utils.PhoneUtils;
import com.offroader.utils.ViewUtils;
import com.viphuli.app.R;
import com.viphuli.common.Constants.ShareType;
import com.viphuli.fragment.AccountLoginFragment;
import com.viphuli.fragment.HomeServiceInfoFragment;

public class JavaScript {
	public static final String NAME = "viphuli";
	private Activity act;

	private JavaScript(Activity act) {
		this.act = act;
	}

	public static JavaScript newInstance(Activity act) {
		return new JavaScript(act);
	}

	@JavascriptInterface
	public void goLogin() {
		AccountLoginFragment.go(act);
	}

	@JavascriptInterface
	public String getUserInfo() {

		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(MyApp.getInstance());
		return tokenKeeper.getOpenId();

	}

	/**
	 * 去服务（上门体检个人,上门护理 个人、上门体检企业）
	 * 
	 * @param serviceItemId
	 */
	@JavascriptInterface
	public void goService(int serviceType) {

		Bundle args = new Bundle();
		args.putInt("service_type", serviceType);

		switch (serviceType) {
		case Constants.HOME_SERVICE_HEALTH_COMPANY:

			MyPageHelper.homeServiceHealth.showMyPage(act, args);

			break;

		case Constants.HOME_SERVICE_NURSE_PERSON:

			MyPageHelper.homeServiceNurse.showMyPage(act, args);

			break;

		case Constants.HOME_SERVICE_HEALTH_PERSON:

			MyPageHelper.homeServiceHealth.showMyPage(act, args);

			break;

		default:
			break;
		}

	}

	/**
	 * 服务项信息
	 */
	@JavascriptInterface
	public void goServiceItem(int serviceItemId, String serviceItemName, int serviceType) {
		HomeServiceInfoFragment.go(act, serviceItemId, serviceType, serviceItemName);
	}

	/**
	 * 私人定制
	 */
	@JavascriptInterface
	public void goPrivateCustom() {
		MyPageHelper.homePrivateCustom.showMyPage(act);
	}

	/**
	 * 定单列表 
	 */
	@JavascriptInterface
	public void goOrderList() {
		Bundle args = new Bundle();
		args.putInt("tab_index", MainTabEnum.order.getIndex());//去订单
		MyPageHelper.main.showMyPage(act, args);
	}

	/**
	 * 定单信息
	 */
	@JavascriptInterface
	public void goOrderInfo(String orderId) {
		Bundle args = new Bundle();
		args.putString("order_id", orderId);
		MyPageHelper.purchaseOrderInfo.showMyPage(act, args);
	}

	/**
	 * 我的
	 */
	@JavascriptInterface
	public void goMy() {
		Bundle args = new Bundle();
		args.putInt("tab_index", MainTabEnum.my.getIndex());//去我的
		MyPageHelper.main.showMyPage(act, args);
	}

	/**
	 * 我的优惠券
	 */
	@JavascriptInterface
	public void goTicket() {
		MyPageHelper.accountTicket.showMyPage(act);
	}

	/**
	 * 护士加入
	 */
	@JavascriptInterface
	public void goNurseJoin() {
		Bundle args = new Bundle();
		MyPageHelper.accountNurseJoinReady.showMyPage(act, args);
	}

	/**
	 * 分享有礼
	 */
	@JavascriptInterface
	public void goShare() {
		MyPageHelper.accountShare.showMyPage(act);
	}

	/**
	 * 分享
	 * @param title
	 * @param content
	 * @param imgUrl
	 * @param url
	 * @param platform
	 */
	@JavascriptInterface
	public void share(String title, String content, String imgUrl, String url, int platform) {

		String appId = MyApp.getInstance().getString(R.string.weichat_app_id);
		String appSecret = MyApp.getInstance().getString(R.string.weichat_app_secret);

		if (ShareType.weichat.getPlatform() == platform)
			ShareUtils.shareToWeiChat(act, appId, appSecret, title, content, imgUrl, url);
		else if (ShareType.weichat_circle.getPlatform() == platform)
			ShareUtils.shareToWeiChatCircle(act, appId, appSecret, title, content, imgUrl, url);
	}

	/**
	 * 关闭当前WEB页面
	 */
	@JavascriptInterface
	public void closePage() {

		act.finish();
	}

	/**
	 * 意见反馈
	 */
	@JavascriptInterface
	public void feedback() {
		MyPageHelper.accountShare.showMyPage(act);
	}

	/**
	 * 客服电话
	 */
	@JavascriptInterface
	public void goServiceTel() {
		ViewUtils.confirm(act, "是否联系客服？", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int arg1) {
				String sereviceTel = OnlineParamUtils.get(Constants.ONLINE_PARAMS_KEY_SERVICE_TEL);
				PhoneUtils.phoneCall(act, sereviceTel);
			}
		});
	}

}
